package widget

import (
	"image/color"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/driver/desktop"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

var _ fyne.Widget = (*BtnX)(nil)

// BtnX is a widget for displaying a separator with themeable color.
//
// Since: 1.4
type BtnX struct {
	// widget.BaseWidget
	widget.DisableableWidget
	text string

	hovered bool
	dt      float32
	// 单击鼠标时执行的动作
	onTapped func() `json:"-"`
	// 双击鼠标时执行的动作
	onDoubleTapped func() `json:"-"`

	onRightTapped func(*fyne.PointEvent) `json:"-"`
	// 鼠标进入该控件时执行的动作
	msin func() `json:"-"`
	// 鼠标离开该控件时执行的动作
	msout func() `json:"-"`

	// 背景
	Bg *canvas.Rectangle
	// 文本
	Textc *canvas.Text
}

// NewBtnX creates a new separator.
//
// Since: 1.4
func NewBtnX(text string) *BtnX {
	s := &BtnX{text: text, dt: theme.InnerPadding() * 2,
		Bg:    canvas.NewRectangle(theme.ForegroundColor()),
		Textc: canvas.NewText(text, theme.BackgroundColor())}
	s.ExtendBaseWidget(s)
	// s.Textc.Alignment = fyne.TextAlignCenter
	return s
}

// CreateRenderer returns a new renderer for the separator.
//
// Implements: fyne.Widget
func (s *BtnX) CreateRenderer() fyne.WidgetRenderer {
	s.ExtendBaseWidget(s)
	ctn := container.NewMax(s.Bg, s.Textc)
	return &btnXRenderer{
		WidgetRenderer: widget.NewSimpleRenderer(ctn),
		d:              s,
	}
}

func (s *BtnX) Do(f func(*BtnX)) *BtnX {
	f(s)
	return s
}

// 设置该控件的文本
func (b *BtnX) T(t string) {
	b.text = t
	b.Refresh()
}

func (b *BtnX) GetT() string {
	return b.text
}

// 设置该控件的文本颜色
func (b *BtnX) Tc(c color.Color) {
	b.Textc.Color = c
	b.Textc.Refresh()
}

// 设置该控件的背景颜色
func (b *BtnX) Bc(c color.Color) {
	b.Bg.FillColor = c
	b.Bg.Refresh()
}

func (b *BtnX) Tap(fn func()) *BtnX {
	b.onTapped = fn
	return b
}
func (b *BtnX) Tap2(fn func()) *BtnX {
	b.onDoubleTapped = fn
	return b
}

func (b *BtnX) Tap3(fn func(e *fyne.PointEvent)) *BtnX {
	b.onRightTapped = fn
	return b
}

// Tapped is called when a pointer tapped event is captured and triggers any tap handler
func (b *BtnX) Tapped(*fyne.PointEvent) {
	if b.Disabled() {
		return
	}

	if b.onTapped != nil {
		b.onTapped()
	}
}

func (b *BtnX) DoubleTapped(*fyne.PointEvent) {
	if b.Disabled() {
		return
	}

	if b.onDoubleTapped != nil {
		b.onDoubleTapped()
	}
}

func (b *BtnX) TappedSecondary(e *fyne.PointEvent) {
	if b.Disabled() {
		return
	}

	if b.onRightTapped != nil {
		b.onRightTapped(e)
	}
}

func (b *BtnX) Msin(fn func()) *BtnX {
	b.msin = fn
	return b
}

// MouseIn is called when a desktop pointer enters the widget
func (b *BtnX) MouseIn(*desktop.MouseEvent) {
	b.hovered = true
	b.Bg.FillColor = theme.PrimaryColor()
	if b.msin != nil {
		b.msin()
	}
	b.Bg.Refresh()

}

// MouseMoved is called when a desktop pointer hovers over the widget
func (b *BtnX) MouseMoved(*desktop.MouseEvent) {
}

// MouseOut is called when a desktop pointer exits the widget
func (b *BtnX) MouseOut() {
	b.hovered = false
	b.Bg.FillColor = theme.ForegroundColor()
	if b.msout != nil {
		b.msout()
	}
	b.Bg.Refresh()
}

// Cursor returns the cursor type of this widget
func (b *BtnX) Cursor() desktop.Cursor {
	return desktop.PointerCursor
}

// MinSize returns the minimal size of the separator.
//
// Implements: fyne.Widget
func (s *BtnX) MinSize() fyne.Size {
	s.ExtendBaseWidget(s)
	// t := theme.BtnXThicknessSize()
	return fyne.MeasureText(s.text, 12, fyne.TextStyle{}).
		AddWidthHeight(s.dt, s.dt)
	// var t float32 = 10
	// return fyne.NewSize(t, t)
}

var _ fyne.WidgetRenderer = (*btnXRenderer)(nil)

type btnXRenderer struct {
	fyne.WidgetRenderer
	d *BtnX
}

func (r *btnXRenderer) Layout(s fyne.Size) {
	r.d.Bg.Resize(s)
	r.d.Textc.Move(fyne.NewPos(theme.InnerPadding(),
		theme.InnerPadding()))
}

func (r *btnXRenderer) MinSize() fyne.Size {
	return fyne.MeasureText(r.d.text, 12, fyne.TextStyle{}).
		AddWidthHeight(r.d.dt, r.d.dt)
	// var t float32 = 10
	// return fyne.NewSize(t, t)
}

func (r *btnXRenderer) Refresh() {
	r.d.Bg.FillColor = theme.ForegroundColor()
	r.d.Textc.Text = r.d.text
	canvas.Refresh(r.d)
}
